package cn.icare.cms.impl;

import cn.icare.cms.CouponService;
import cn.icare.cms.ListUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.CouponMapper;
import cn.icare.cms.dao.mapper.CustomMapper;
import cn.icare.cms.dao.mapper.MemberCouponMapper;
import cn.icare.cms.dao.po.Coupon;
import cn.icare.cms.dao.po.MemberCoupon;
import cn.icare.cms.model.enums.CommonStatusEnum;
import cn.icare.cms.model.ro.AddCouponRO;
import cn.icare.cms.model.ro.EditCouponRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl implements CouponService {


    @Resource
    private CouponMapper couponMapper;

    @Resource
    private MemberCouponMapper memberCouponMapper;

    @Resource
    private CustomMapper customMapper;


    private final Logger logger = LoggerFactory.getLogger(CouponServiceImpl.class);


    @Override
    public Integer createOne(AddCouponRO ro, String actionUserName) {
        try {
            Coupon ad = new Coupon();
            ad.setName(ro.getName());
            ad.setType(ro.getType());
            ad.setNum(ro.getNum());
            ad.setNeednum(ro.getNeednum());
            ad.setCreator(actionUserName);
            ad.setCreatetime(new Date());
            ad.setStatus(ro.getStatus());//正常
            ad.setTotal(ro.getTotal());
            ad.setGettotal(0);
            ad.setStarttime(ro.getStarttime());
            ad.setEndtime(ro.getEndTime());
            int result = this.couponMapper.insertSelective(ad);
            if (result > 0) {
                return ad.getId();
            }
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return 0;
    }

    @Override
    public boolean updateOne(EditCouponRO ro, String actionUserName) {
        try {
            Coupon ad = new Coupon();
            ad.setId(ro.getId());
            ad.setName(ro.getName());
            ad.setType(ro.getType());
            ad.setNum(ro.getNum());
            ad.setNeednum(ro.getNeednum());
            ad.setCreator(actionUserName);
            ad.setCreatetime(new Date());
            ad.setStatus(CommonStatusEnum.EABLED.getCode());//正常
            int result = this.couponMapper.insertSelective(ad);
            return result > 0;
        } catch (Exception ex) {
            this.logger.error("createOne异常", ex);
        }
        return false;
    }

    @Override
    public boolean deleteOne(Integer id) {
        int result = this.couponMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Coupon.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.couponMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Coupon getOne(Integer id) {
        return this.couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(String name, Integer type, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(Coupon.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" + name + "%");
            }
            if (type != null && type > 0) {
                criteria.andEqualTo("type", type);
            }

            example.setOrderByClause("id desc");
            List<Coupon> dataList = this.couponMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public boolean updateOneGetTotal(Integer id, Integer getTotal) {
        Coupon coupon = new Coupon();
        coupon.setId(id);
        coupon.setGettotal(getTotal);
        int result = this.couponMapper.updateByPrimaryKeySelective(coupon);
        return result > 0;
    }

    @Override
    public boolean updateOneStatus(Integer id, Integer status) {
        Coupon coupon = new Coupon();
        coupon.setId(id);
        coupon.setStatus(status);
        int result = this.couponMapper.updateByPrimaryKeySelective(coupon);
        if(result>0 && status==CommonStatusEnum.DISABLED.getCode())
        {
            //强制将所有已经领取的会员优惠券失效

            Example example = new Example(MemberCoupon.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("couponid",id);
            MemberCoupon memberCoupon=new MemberCoupon();
            memberCoupon.setCouponid(id);
            memberCoupon.setStatus(2);
            this.memberCouponMapper.updateByExampleSelective(memberCoupon,example);
        }
        return result > 0;
    }

    @Override
    public MsgDatasVO getCanUseList(Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        MsgDatasVO result = new MsgDatasVO();
        try {

            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("select * from t_coupon where `status`=1 and  endtime>NOW() " +
                    " and total>gettotal " +
                    " order by starttime asc");
            List<Map<String,Object>> dataList = this.customMapper.select(stringBuffer.toString());
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getCanUseList 异常", e);
        }
        return result;
    }

    @Override
    public List<Map<String,Object>>  getOnlyNewMemberList() {
        List<Map<String,Object>> dataList =new ArrayList<>();
        try {

            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("select id,starttime,endtime,gettotal from t_coupon where `status`=1 and type=1 and  endtime>NOW() " +
                    " and total>gettotal " +
                    "");
           dataList = this.customMapper.select(stringBuffer.toString());


        } catch (Exception e) {
            this.logger.error("getOnlyNewMemberList 异常", e);
        }
        return dataList;
    }
}
